library(foreign)
library(readstata13)
library(dplyr)
library(ltm)


rm(list=ls())

#download all data####
nbps198488 <- read.dta13("NBES_8488.dta")
nbps1993 <- read.dta("NBPS_1993.dta")
nbes1996 <- read.dta("NBES_1996.dta")
anes2004 <- read.dta("ANES_2004.dta")
anes2008 <- read.dta13("ANES_2008.dta")
anes2012 <- read.dta13("ANES_2012.dta")
anes2016 <- read.dta("ANES_2016.dta")
anes20 <- read.csv("ANES_2020.csv")

#National Black Election Study 1984, 1988 -----

##racial group consciousness----
#discrimination against Black people is no longer a problem
nbps198488$disc<-nbps198488$v2093
nbps198488$disc[nbps198488$disc == 1] <- 0
nbps198488$disc[nbps198488$disc == 2] <- 1
nbps198488$disc[nbps198488$disc == 3] <- 2
nbps198488$disc[nbps198488$disc == 4] <- 3
nbps198488$disc[nbps198488$disc == 5] <- 4
nbps198488$disc1 <- nbps198488$disc / 4

#over last 20 years hasn't been much change
nbps198488$disca <- nbps198488$v2138
nbps198488$disca[nbps198488$disca == 1] <- 0
nbps198488$disca[nbps198488$disca == 5] <- 1

#Kept Back because of their race 84
nbps198488$sysb84 <- nbps198488$v2143
nbps198488$sysb84[nbps198488$sysb84 == 1] <- 0
nbps198488$sysb84[nbps198488$sysb84 == 2] <- 1
nbps198488$sysb84[nbps198488$sysb84 >= 6] <- 0

#kept back bc of race 88
nbps198488$sysb88 <- nbps198488$v4035
nbps198488$sysb88[nbps198488$sysb88 == 2] <- 0.66
nbps198488$sysb88[nbps198488$sysb88 == 3] <- 0.33
nbps198488$sysb88[nbps198488$sysb88 == 4] <- 0

#Black group influence
nbps198488$binf88 <- nbps198488$v3141
nbps198488$binf88[nbps198488$binf88 == 6] <- 1
nbps198488$binf88[nbps198488$binf88 == 5] <- .5
nbps198488$binf88[nbps198488$binf88 == 3] <- 0

#commonalities among Black respondents (84 + 88)
nbps198488$centb84 <- nbps198488$v2141
nbps198488$centb84[nbps198488$centb84 == 2] <- .66
nbps198488$centb84[nbps198488$centb84 == 4] <- .33
nbps198488$centb84[nbps198488$centb84 >= 5] <- 0

nbps198488$centb88 <- nbps198488$v3152
nbps198488$centb88[nbps198488$centb88 == 2] <- .66
nbps198488$centb88[nbps198488$centb88 == 4] <- .33
nbps198488$centb88[nbps198488$centb88 >= 5] <- 0

#Black or American Identity?
nbps198488$bam <- nbps198488$v2142
nbps198488$bam[nbps198488$bam >= 2] <- 0

#power/collective-action
nbps198488$bcoll <- nbps198488$v2144
nbps198488$bcoll[nbps198488$bcoll >= 2] <- 0

#efficacy / vote power, president###
nbps198488$bvotep4 <- nbps198488$v3125
nbps198488$bvotep4[nbps198488$bvotep4 == 2] <- 0.66
nbps198488$bvotep4[nbps198488$bvotep4 == 3] <- 0.33
nbps198488$bvotep4[nbps198488$bvotep4 == 4] <- 0

nbps198488$bvotep8 <- nbps198488$v4029
nbps198488$bvotep8[nbps198488$bvotep8 == 2] <- 0.66
nbps198488$bvotep8[nbps198488$bvotep8 == 3] <- 0.33
nbps198488$bvotep8[nbps198488$bvotep8 == 4] <- 0

#black people can make a difference in local election
nbps198488$bvotel4 <- nbps198488$v3126
nbps198488$bvotel4[nbps198488$bvotel4 == 2] <- 0.66
nbps198488$bvotel4[nbps198488$bvotel4 == 3] <- 0.33
nbps198488$bvotel4[nbps198488$bvotel4 == 4] <- 0

nbps198488$bvotel8 <- nbps198488$v4030
nbps198488$bvotel8[nbps198488$bvotel8 == 2] <- 0.66
nbps198488$bvotel8[nbps198488$bvotel8 == 3] <- 0.33
nbps198488$bvotel8[nbps198488$bvotel8 == 4] <- 0

#black influence
nbps198488$binf88 <- nbps198488$v3141
nbps198488$binf88[nbps198488$binf88 >= 8] <- 0
nbps198488$binf88[nbps198488$binf88 == 1] <- 0
nbps198488$binf88[nbps198488$binf88 == 3] <- 0.33
nbps198488$binf88[nbps198488$binf88 == 5] <- .66
nbps198488$binf88[nbps198488$binf88 == 6] <- 1

nbps198488$binf84 <- nbps198488$v1099
nbps198488$binf84[nbps198488$binf84 >= 8] <- 0
nbps198488$binf84[nbps198488$binf84 == 1] <- 0
nbps198488$binf84[nbps198488$binf84 == 3] <- 0.50
nbps198488$binf84[nbps198488$binf84 == 5] <- 1

#bclose
nbps198488$bclose <- nbps198488$v2153
nbps198488$bclose[nbps198488$bclose == 4] <- 0
nbps198488$bclose[nbps198488$bclose == 2] <- 0.66
nbps198488$bclose[nbps198488$bclose == 3] <- 0.3

#bcana
nbps198488$bcana <- nbps198488$v4062
nbps198488$bcana[nbps198488$bcana == 4] <- 0
nbps198488$bcana[nbps198488$bcana == 2] <- 0.66
nbps198488$bcana[nbps198488$bcana == 3] <- 0.3

#brig4/8
nbps198488$brig4 <- nbps198488$v1107
nbps198488$brig4[nbps198488$brig4 == 5] <- 0
nbps198488$brig8 <- nbps198488$v3150
nbps198488$brig8[nbps198488$brig8 == 5] <- 0


#linked/common fate
nbps198488$blf<-nbps198488$v5025
nbps198488$blf<-(nbps198488$blf-3) / 12

#rgc for both 1984 and 1988
nbps198488$rgcmean <- (rowMeans(nbps198488[c("disc1", "disca", "sysb88", "sysb84", "binf84", 
                                             "binf88", "centb84", "centb88", "bam", "bcoll", "bvotep4", "bvotep8", "bvotel4" ,"bvotel8", "binf84",
                                             "binf88", "bclose", "blf", "brig4", "brig8")], na.rm=TRUE))

#rgc for 1984
nbps198488$rgc84 <- (rowMeans(nbps198488[c("disc1", "disca", "sysb84", "binf84", 
                                           "centb84", "bam", "bcoll", "bvotep4", "bvotel4", "binf84",
                                           "bclose", "blf", "brig4")], na.rm=TRUE))
#rgc for 1988
nbps198488$rgc88 <- (rowMeans(nbps198488[c("disc1", "disca", "sysb88", "binf88", 
                                           "centb88", "bam", "bcoll", "bvotep8", "bvotel8", "binf88",
                                           "bclose", "blf", "brig8")], na.rm=TRUE))
summary(nbps198488$rgcmean)
summary(nbps198488$rgc84)
summary(nbps198488$rgc88)

#cronbach alpha of rgc variable
myvars84 <- c("disc1", "disca", "sysb84", "binf84", 
              "centb84", "bam", "bcoll", "bvotep4", "bvotel4" , "binf84",
              "bclose", "blf", "brig4")
myvars88 <- c("disc1", "disca", "sysb88", 
              "binf88", "centb88", "bam", "bcoll", "bvotep8" ,"bvotel8",
              "binf88", "bclose", "blf", "brig8")

nd84 <- nbps198488[myvars84]
nd88 <- nbps198488[myvars88]

cronbach.alpha(nd84, na.rm = TRUE)
cronbach.alpha(nd88, na.rm = TRUE)

##political participation----
#campacto8
nbps198488$campacto8 <- nbps198488$v5001
nbps198488$campacto8[nbps198488$campacto8 == 1] <- 5
nbps198488$campacto8[nbps198488$campacto8 == 2] <- 4
nbps198488$campacto8[nbps198488$campacto8 == 4] <- 2
nbps198488$campacto8[nbps198488$campacto8 == 5] <- 1

#talk about politics
nbps198488$talk8 <- nbps198488$v4020
nbps198488$talk8[nbps198488$talk8 == 0] <- NA
nbps198488$talk8[nbps198488$talk8 == 5] <- 0
nbps198488$talk8[nbps198488$talk8 >= 8 ] <- 0

#rallies
nbps198488$rally8 <- nbps198488$v4021
nbps198488$rally8[nbps198488$rally8 == 0] <- NA
nbps198488$rally8[nbps198488$rally8 == 5] <- 0
nbps198488$rally8[nbps198488$rally8 >= 8 ] <- 0

#reg
nbps198488$reg8 <- nbps198488$v4022
nbps198488$reg8[nbps198488$reg8 == 0] <- NA
nbps198488$reg8[nbps198488$reg8 == 5] <- 0
nbps198488$reg8[nbps198488$reg8 >= 8 ] <- 0

##give money
nbps198488$money8 <- nbps198488$v4023
nbps198488$money8[nbps198488$money8 == 0] <- NA
nbps198488$money8[nbps198488$money8 == 5] <- 0
nbps198488$money8[nbps198488$money8 >= 8 ] <- 0

#campb8
nbps198488$campb8 <- nbps198488$v4024
nbps198488$campb8[nbps198488$campb8 == 0] <- NA
nbps198488$campb8[nbps198488$campb8 == 5] <- 0
nbps198488$campb8[nbps198488$campb8 >= 8 ] <- 0

#vote8
nbps198488$vote8 <- nbps198488$v4007
nbps198488$vote8[nbps198488$vote8 == 0] <- NA
nbps198488$vote8[nbps198488$vote8 == 5] <- 0
nbps198488$vote8[nbps198488$vote8 >= 8 ] <- 0

#campact88
nbps198488$campact88a <- (nbps198488$talk8 + nbps198488$rally8 + nbps198488$reg8 + nbps198488$money8 + nbps198488$campb8 + nbps198488$vote8)/6

#talk4
nbps198488$talk4 <- nbps198488$v2086
nbps198488$talk4[nbps198488$talk4 == 0] <- NA
nbps198488$talk4[nbps198488$talk4 == 5] <- 0
nbps198488$talk4[nbps198488$talk4 >= 8 ] <- 0

#rally4
nbps198488$rally4 <- nbps198488$v2087
nbps198488$rally4[nbps198488$rally4 == 0] <- NA
nbps198488$rally4[nbps198488$rally4 == 5] <- 0
nbps198488$rally4[nbps198488$rally4 >= 8 ] <- 0

#reg4
nbps198488$reg4 <- nbps198488$v2088
nbps198488$reg4[nbps198488$reg4 == 0] <- NA
nbps198488$reg4[nbps198488$reg4 == 5] <- 0
nbps198488$reg4[nbps198488$reg4 >= 8 ] <- 0

#money4
nbps198488$money4 <- nbps198488$v2089
nbps198488$money4[nbps198488$money4 == 0] <- NA
nbps198488$money4[nbps198488$money4 == 5] <- 0
nbps198488$money4[nbps198488$money4 >= 8 ] <- 0

#campb4
nbps198488$campb4 <- nbps198488$v2090
nbps198488$campb4[nbps198488$campb4 == 0] <- NA
nbps198488$campb4[nbps198488$campb4 == 5] <- 0
nbps198488$campb4[nbps198488$campb4 >= 8 ] <- 0


#vote4
nbps198488$vote4 <- nbps198488$v2062
nbps198488$vote4[nbps198488$vote4 == 0] <- NA
nbps198488$vote4[nbps198488$vote4 == 5] <- 0
nbps198488$vote4[nbps198488$vote4 >= 8 ] <- 0

#campact
nbps198488$campact84a <- (nbps198488$talk4 + nbps198488$rally4 + nbps198488$reg4 + nbps198488$money4 + nbps198488$campb4 + nbps198488$vote4)/6

#mean 84/88 participation
summary(nbps198488$campact84a)
summary(nbps198488$campact88a)

#cronbach of campaign activity variable
myvars84 <- c("talk4", "rally4", "reg4", "money4", 
              "campb4", "vote4")
myvars88 <- c("talk8", "rally8", "reg8", "money8", 
              "campb8", "vote8")

nd84 <- nbps198488[myvars84]
nd88 <- nbps198488[myvars88]

cronbach.alpha(nd84, na.rm = TRUE)
cronbach.alpha(nd88, na.rm = TRUE)

#standardize by dividing the proportion of campaign activities by the standard deviation
nbps198488$campact84standardized <- (nbps198488$campact84a - 0.3290398) / (sd(nbps198488$campact84a))
nbps198488$campact88standardized <- (nbps198488$campact88a - 0.3431085)/ (sd(nbps198488$campact88a, na.rm = TRUE))


#age
nbps198488$age <- nbps198488$v1119

#education
nbps198488$edu84 <- nbps198488$v1135
nbps198488$edu84 <- (nbps198488$edu84 - 1)/5

nbps198488$edu88 <- nbps198488$v3260
nbps198488$edu88 <- (nbps198488$edu88 - 1)/5

#income
nbps198488$inc <- nbps198488$v2196
nbps198488$inc[nbps198488$inc == 5000] <- 0
nbps198488$inc[nbps198488$inc == 10000] <- 1
nbps198488$inc[nbps198488$inc == 15000] <- 2
nbps198488$inc[nbps198488$inc == 20000] <- 3
nbps198488$inc[nbps198488$inc == 25000] <- 4
nbps198488$inc[nbps198488$inc == 30000] <- 5
nbps198488$inc[nbps198488$inc == 35000] <- 6
nbps198488$inc[nbps198488$inc == 60000] <- 7
nbps198488$inc <- nbps198488$inc/7

#sex
nbps198488$sex <- nbps198488$v1231
nbps198488$sex <- nbps198488$sex - 1

#att1
nbps198488$att1 <- nbps198488$v3039
nbps198488$att1[nbps198488$att1 == 5]<- 0
nbps198488$att1[nbps198488$att1 == 4]<- 0.25
nbps198488$att1[nbps198488$att1 == 3]<- 0.50
nbps198488$att1[nbps198488$att1 == 2]<- 0.75

#att2#
nbps198488$att2 <- nbps198488$v3037
nbps198488$att2[nbps198488$att2 == 5]<- 0
nbps198488$att2[nbps198488$att2 == 4]<- 0.25
nbps198488$att2[nbps198488$att2 == 3]<- 0.50
nbps198488$att2[nbps198488$att2 == 2]<- 0.75

#inter#
nbps198488$inter <- nbps198488$v1021
nbps198488$inter[nbps198488$inter == 3] <- .5
nbps198488$inter[nbps198488$inter == 5] <- 0

#inter88#
nbps198488$inter88 <- nbps198488$v2047
nbps198488$inter88[nbps198488$inter88 == 3] <- .5
nbps198488$inter88[nbps198488$inter88 == 5] <- 0

#attsc

nbps198488$attsc <- (rowMeans(nbps198488[c("inter", "inter88", "att1", "att2")], na.rm=TRUE))

#religiosity
nbps198488$relig84 <- nbps198488$v1181
nbps198488$relig84 <- ifelse(nbps198488$relig84 == 9, NA, nbps198488$relig84)
nbps198488$relig84 <- (nbps198488$relig84 -1)/4

nbps198488$relig88 <- nbps198488$v3211
nbps198488$relig88 <- (nbps198488$relig88 -1)/4

#create ses variable using homeownership#
nbps198488$homeowner84<-0
nbps198488$homeowner84[nbps198488$v2186 == 1] <- 1

nbps198488$homeowner88<-0
nbps198488$homeowner88[nbps198488$v4076 == 1] <- 1

nbps198488$ses84 <- (nbps198488$inc + nbps198488$edu84 + nbps198488$homeowner84) /3
nbps198488$ses88 <- (nbps198488$inc  + nbps198488$edu88 + nbps198488$homeowner88) / 3

##create final datasets for 1984 and 1988----
final1984 <- nbps198488 %>%
  dplyr::select(attsc, sex, inc, edu84, age, rgc84, relig84, campact84a, campact84standardized, vote4, 
                ses84, money4, blf)
final1988 <- nbps198488 %>%
  dplyr::select(attsc, sex, inc, edu88, age, rgc88, relig88, campact88a, campact88standardized, vote8,
                money8, ses88, blf)

#use same variable names for all years
final1984 <- final1984 %>%
  rename(polint = attsc,
         female = sex,
         edu = edu84,
         rgc = rgc84,
         relig = relig84,
         campact = campact84a,
         campactstandard = campact84standardized,
         turnout = vote4,
         ses = ses84,
         donate = money4)

final1984$year <- rep("1984", 854)

final1988 <- final1988 %>%
  rename(polint = attsc,
         female = sex,
         edu = edu88,
         rgc = rgc88,
         relig = relig88,
         campact = campact88a,
         campactstandard = campact88standardized,
         turnout = vote8,
         ses = ses88,
         donate = money8)

final1988$year <- rep("1988", 854)

#National Black Politics Study 1993 ----

##political participation----


table(nbps1993$B7)
nbps1993$B7 <- ifelse(nbps1993$B7 == "Don't know" | nbps1993$B7 == "Refused/NA", NA, nbps1993$B7)
nbps1993$vote <- nbps1993$B7  
nbps1993$vote <- ifelse(nbps1993$vote == 1, 1, 0)

table(nbps1993$E22)
nbps1993$E22 <- ifelse(nbps1993$E22 == "Don't know" | nbps1993$E22 == "Refused/NA", NA, nbps1993$E22)
nbps1993$donate <- nbps1993$E22
nbps1993$donate <- ifelse(nbps1993$donate == 1, 1, 0)

table(nbps1993$E20)
nbps1993$E20 <- ifelse(nbps1993$E20 == "Don't know" | nbps1993$E20 == "Refused/NA", NA, nbps1993$E20)
nbps1993$registration <- nbps1993$E20
nbps1993$registration <- ifelse(nbps1993$registration == 1, 1, 0)

table(nbps1993$E24)
nbps1993$E24 <- ifelse(nbps1993$E24 == "Don't know" | nbps1993$E24 == "Refused/NA", NA, nbps1993$E24)
nbps1993$campaign <- nbps1993$E24
nbps1993$campaign <- ifelse(nbps1993$campaign == 1, 1, 0)

table(nbps1993$E25)
nbps1993$E25 <- ifelse(nbps1993$E25 == "Don't know" | nbps1993$E25 == "Refused/NA", NA, nbps1993$E25)
nbps1993$petition <- nbps1993$E25
nbps1993$petition <- ifelse(nbps1993$petition == 1, 1, 0)

table(nbps1993$E23)
nbps1993$E23 <- ifelse(nbps1993$E23 == "Don't know" | nbps1993$E23 == "Refused/NA", NA, nbps1993$E23)
nbps1993$fundraise <- nbps1993$E23
nbps1993$fundraise <- ifelse(nbps1993$fundraise == 1, 1, 0)

table(nbps1993$E21)
nbps1993$E21 <- ifelse(nbps1993$E21 == "Don't know" | nbps1993$E21 == "Refused/NA", NA, nbps1993$E21)
nbps1993$ride <- nbps1993$E21
nbps1993$ride <- ifelse(nbps1993$ride == 1, 1, 0)

nbps1993$pol_part <- (nbps1993$vote + nbps1993$donate + nbps1993$registration + nbps1993$campaign 
                      + nbps1993$petition + nbps1993$fundraise + nbps1993$ride)/7


polpart93 <- c("vote", "donate", "registration", "campaign", "petition", "fundraise", "ride")

polpart93 <- nbps1993[polpart93]

cronbach.alpha(polpart93, na.rm = TRUE)

#standardize political participation by dividing the proportion of 
#campaign activities by the standard deviation
nbps1993$pol_partstandard <- (nbps1993$pol_part - mean(nbps1993$pol_part, na.rm = TRUE))/ 
  (sd(nbps1993$pol_part, na.rm = TRUE))

##racial group consciousness----

#linked fate
table(nbps1993$D1)
nbps1993$linkedfate<-as.numeric(nbps1993$D1)

nbps1993$linkedfate[nbps1993$linkedfate == 2] <- 0
nbps1993$linkedfate[nbps1993$linkedfate == 1] <- 1
nbps1993$linkedfate[nbps1993$linkedfate > 2] <- NA
nbps1993$linkedfate[nbps1993$D1A == "Not very much"] <- .33
nbps1993$linkedfate[nbps1993$D1A == "Some"] <- .66
nbps1993$linkedfate[nbps1993$D1A == "A lot"] <- 1
nbps1993$linkedfate[nbps1993$D1A == "Don't know"] <- .66
nbps1993$linkedfate[nbps1993$D1A == "Refused/NA"] <- .66

table(nbps1993$F13)
nbps1993$economic_divisions <-as.numeric(nbps1993$F13)

nbps1993$economic_divisions[nbps1993$economic_divisions == 1] <- 0
nbps1993$economic_divisions[nbps1993$economic_divisions == 3] <- 0
nbps1993$economic_divisions[nbps1993$economic_divisions == 4] <- 0
nbps1993$economic_divisions[nbps1993$economic_divisions == 2] <- 1
nbps1993$economic_divisions[nbps1993$economic_divisions == 5] <- NA
nbps1993$economic_divisions[nbps1993$economic_divisions == 6] <- NA


table(nbps1993$F15)
nbps1993$corporations <-as.numeric(nbps1993$F15)

nbps1993$corporations[nbps1993$corporations == 1] <- 0
nbps1993$corporations[nbps1993$corporations == 2] <- 1
nbps1993$corporations[nbps1993$corporations == 3] <- 0
nbps1993$corporations[nbps1993$corporations == 4] <- 0
nbps1993$corporations[nbps1993$corporations == 5] <- NA
nbps1993$corporations[nbps1993$corporations == 6] <- NA

table(nbps1993$F22)
nbps1993$immigrants <-as.numeric(nbps1993$F22)

nbps1993$immigrants[nbps1993$immigrants == 1] <- 0
nbps1993$immigrants[nbps1993$immigrants == 2] <- 1
nbps1993$immigrants[nbps1993$immigrants == 3] <- 0
nbps1993$immigrants[nbps1993$immigrants == 4] <- 0
nbps1993$immigrants[nbps1993$immigrants == 5] <- NA
nbps1993$immigrants[nbps1993$immigrants == 6] <- NA

table(nbps1993$F25)
nbps1993$racial_equality <- as.numeric(nbps1993$F25)
nbps1993$racial_equality <- nbps1993$racial_equality - 1
nbps1993$racial_equality[nbps1993$racial_equality == 4] <- NA
nbps1993$racial_equality[nbps1993$racial_equality == 5] <- NA
nbps1993$racial_equality <- nbps1993$racial_equality/3

table(nbps1993$G9)
nbps1993$black_ft <- nbps1993$G9
nbps1993$black_ft[nbps1993$black_ft > 100] <- NA
nbps1993$black_ft <- nbps1993$black_ft/100

table(nbps1993$F6)
nbps1993$racial_progress <-as.numeric(nbps1993$F6)

nbps1993$racial_progress[nbps1993$racial_progress == 1] <- 0
nbps1993$racial_progress[nbps1993$racial_progress == 2] <- 1
nbps1993$racial_progress[nbps1993$racial_progress == 3] <- 0
nbps1993$racial_progress[nbps1993$racial_progress == 4] <- 0
nbps1993$racial_progress[nbps1993$racial_progress == 5] <- NA
nbps1993$racial_progress[nbps1993$racial_progress == 6] <- NA


table(nbps1993$F9)
nbps1993$american_society <-as.numeric(nbps1993$F9)

nbps1993$american_society[nbps1993$american_society == 1] <- 0
nbps1993$american_society[nbps1993$american_society == 2] <- 1
nbps1993$american_society[nbps1993$american_society == 3] <- 0
nbps1993$american_society[nbps1993$american_society == 4] <- 0
nbps1993$american_society[nbps1993$american_society == 5] <- NA
nbps1993$american_society[nbps1993$american_society == 6] <- NA

d <- nbps1993 %>%
  dplyr::select(linkedfate, economic_divisions, corporations, immigrants, racial_equality, racial_progress,
                black_ft, american_society)

nbps1993$rgc <- (rowMeans(nbps1993[c("linkedfate", "economic_divisions", "corporations", "immigrants",
                                     "racial_equality", "racial_progress", "black_ft", "american_society")], 
                          na.rm = TRUE))

rgc93 <- c(c("linkedfate", "economic_divisions", "corporations", "immigrants",
                 "racial_equality", "racial_progress", "black_ft", "american_society"))
rgc93 <- nbps1993[rgc93]

cronbach.alpha(rgc93, na.rm = TRUE)

##demographics----
table(nbps1993$J16)
nbps1993$inc <- as.numeric(nbps1993$J16)
nbps1993$inc[nbps1993$inc == 11] <- NA
nbps1993$inc <- nbps1993$inc - 1
nbps1993$inc <- nbps1993$inc/9

nbps1993$female <- 0
nbps1993$female[nbps1993$A10 == "FEMALE"] <- 1

nbps1993$age <- nbps1993$A14
nbps1993$age[nbps1993$age == 999] <- NA

nbps1993$educ <- nbps1993$J2
nbps1993$educ[nbps1993$educ >= 98] <- NA
nbps1993$edu <- nbps1993$edu/26

nbps1993$polint <- 0 
nbps1993$polint[nbps1993$I5 == "Yes"] <- 1

nbps1993$relig[nbps1993$E4 == "None" | nbps1993$E4 == "Other" | nbps1993$E4 == "Don't know" | nbps1993$E4 == "Refused/NA"] <- 0
nbps1993$relig[nbps1993$E7 == "Never"] <- 0.25
nbps1993$relig[nbps1993$E7 == "Once or twice a year"] <- 0.5
nbps1993$relig[nbps1993$E7 == "Once or twice a month"] <- 0.75
nbps1993$relig[nbps1993$E7 == "At least once a week"] <- 1

#ses
nbps1993$homeowner <- 0
nbps1993$homeowner[nbps1993$J17 == "Own or buying"] <- 1
nbps1993$ses <- (nbps1993$homeowner + nbps1993$educ + nbps1993$inc ) / 3

##final dataset for 1993----
final1993 <- nbps1993 %>%
  dplyr::select(pol_part, pol_partstandard, rgc, inc, female, age, educ, polint, relig,
                vote, donate, ses, linkedfate)

#use same variable names for all years
final1993 <- final1993 %>%
  rename(edu = educ,
         campact = pol_part,
         campactstandard = pol_partstandard,
         turnout = vote,
         blf = linkedfate)

final1993$year <- rep("1992", 1206)

#National Black Election Study 1996----

##political participation-----

#turnout
table(nbes1996$QA4) 

nbes1996 <- nbes1996 %>%
  mutate(turnout = as.numeric(QA4), 
         turnout = if_else(turnout == 1, 1, 0)) #coded DK as no

#give money
table(nbes1996$E3D) #give or help raise money
table(nbes1996$F5) #give money to a political party
table(nbes1996$F6) #give money to another group

nbes1996 <- nbes1996 %>%
  mutate(donate = as.numeric(E3D),
         donate = ifelse(donate == 3, NA, #make refused NA
                         ifelse(donate == 2, 0, #No is 0
                                ifelse(donate == 1, 1, donate))), #yes is 1
         moneyp = as.numeric(F5),
         moneyp = ifelse(moneyp == 4, NA, #make refused NA
                         ifelse(moneyp == 2, 0, moneyp)),#no is 0, yes is 1
         moneyg = as.numeric(F6),
         moneyg = ifelse(moneyg == 3, NA, #make refused NA
                         ifelse(moneyg == 4, NA, #make don't know nA
                                ifelse(moneyg == 2, 0,
                                       ifelse(moneyg == 1, 1, moneyg)))) #no is 0 , yes is 1
  )

#work
table(nbes1996$E3C) #help with voter registration drive/get people to polls
table(nbes1996$E3E) #campaign for a Black candidate

nbes1996 <- nbes1996 %>%
  mutate(reg_polls = as.numeric(E3C),
         reg_polls = ifelse(reg_polls == 3, NA, #make refused NA
                            ifelse(reg_polls == 2, 0, reg_polls)), #no is 0, yes is 1
         camp_black = as.numeric(E3E),
         camp_black = ifelse(camp_black == 3, NA, #make refused NA
                             ifelse(camp_black == 2, 0, camp_black)) #no is 0, yes is 1 
  )

#talk politics
table(nbes1996$E3A) #talk to people
table(nbes1996$E3B) #rally

nbes1996 <- nbes1996 %>%
  mutate(talk = as.numeric(E3A),
         talk = ifelse(talk == 3, NA, #make refused NA
                       ifelse(talk == 2, 0, talk)), #no is 0, yes is 1
         rally = as.numeric(E3B),
         rally = ifelse(rally == 3, NA, #make refused NA
                        ifelse(rally == 2, 0, rally)) #no is 0, yes is 1 
  )

#political participation variable

nbes1996$pol_part <- (nbes1996$turnout + nbes1996$donate + nbes1996$moneyp + nbes1996$moneyg + nbes1996$reg_polls +
                        nbes1996$camp_black + nbes1996$talk + nbes1996$rally)/8

#standardize by dividing the proportion of campaign activities by the standard deviation
nbes1996$pol_partstandard <- (nbes1996$pol_part - mean(nbes1996$pol_part, na.rm = TRUE))/ 
  (sd(nbes1996$pol_part, na.rm = TRUE))


#cronbach alpha of political participation variable
d <- nbes1996
d <- d %>%
  dplyr::select(turnout, donate, moneyp, moneyg, reg_polls, camp_black, talk, rally)

cronbach.alpha(d, na.rm = TRUE)

##rgc------

table(nbes1996$QV1)

nbes1996$linkb[nbes1996$QV1 == "NO"] <- 0
nbes1996$linkb[nbes1996$QV1 == "DK"] <- NA
nbes1996$linkb[nbes1996$QV1 == "REFUSED/NA"] <-NA
nbes1996$linkb[nbes1996$QV1A == "NOT VERY MUCH"] <- .33
nbes1996$linkb[nbes1996$QV1A == "SOME"] <- .66
nbes1996$linkb[nbes1996$QV1A == "A LOT"] <- 1
nbes1996$linkb[nbes1996$QV1A == "DK"] <- .66
nbes1996$linkb[nbes1996$QV1A == "REFUSED/NA"] <- .66

table(nbes1996$QS8A2)
nbes1996$imp <- as.numeric(nbes1996$QS8A2)
nbes1996$imp[nbes1996$imp == 6] <- NA
nbes1996$imp[nbes1996$imp == 7] <- NA
nbes1996$imp <- 5 - nbes1996$imp
nbes1996$imp <- nbes1996$imp/4

#black feeling thermometer
table(nbes1996$B1N)

nbes1996 <- nbes1996 %>%
  mutate(bft = B1N,
         bft = ifelse(bft > 100, NA, bft), #take out NA
         bft = bft/100) #range from 0 to 1

#progress ending racial discrimination in past 20 years
table(nbes1996$E5)

nbes1996$racial_progress <- as.numeric(nbes1996$E5)
nbes1996$racial_progress[nbes1996$racial_progress == 1] <- 0 #make yes 0
nbes1996$racial_progress[nbes1996$racial_progress == 2] <- 1 #make no 1
nbes1996$racial_progress[nbes1996$racial_progress == 3] <- 0.5 #make dk 0.5
nbes1996$racial_progress[nbes1996$racial_progress == 4] <- NA #make refused na


#if enough Black Americans vote, they can make a difference in who gets elected president
table(nbes1996$E1A)
nbes1996$vote_pres <- as.numeric(nbes1996$E1A)
nbes1996$vote_pres[nbes1996$vote_pres == 5] <- NA #make not sure/don't know NA
nbes1996$vote_pres <- 4 - nbes1996$vote_pres #reverse code so agree strongly is 3
nbes1996$vote_pres <- nbes1996$vote_pres/3 #range from 0 to 1

#Black people can make a difference in who gets elected to Congress

table(nbes1996$E1D)
nbes1996$vote_cong <- as.numeric(nbes1996$E1D)
nbes1996$vote_cong[nbes1996$vote_cong == 5] <- NA #make not sure/don't know NA
nbes1996$vote_cong[nbes1996$vote_cong == 6] <- NA #make refused NA
nbes1996$vote_cong <- 4 - nbes1996$vote_cong #reverse code so agree strongly is 3
nbes1996$vote_cong <- nbes1996$vote_cong/3 #range from 0 to 1

nbes1996$rgc <- (rowMeans(nbes1996[c("vote_cong", "vote_pres", "racial_progress", "bft",
                                     "imp", "linkb")], na.rm=TRUE))
#cronbach alpha of rgc variable
d2 <- nbes1996
d2 <- d2 %>%
  dplyr::select(vote_cong, vote_pres, racial_progress, bft, imp, linkb)

cronbach.alpha(d2, na.rm = TRUE)

d2$rgc <- rowMeans(d2, na.rm = TRUE) 

##demographics----
table(nbes1996$QY6)
nbes1996$inc <- as.numeric(nbes1996$QY6)
nbes1996$inc[nbes1996$inc > 11] <- NA
nbes1996$inc <- nbes1996$inc - 1
nbes1996$inc <- nbes1996$inc/10


nbes1996$age <- nbes1996$QY1C
nbes1996$age[nbes1996$age == 0] <- NA

nbes1996$female <- 0
nbes1996$female[nbes1996$QZ2 == "FEMALE"] <- 1

nbes1996$education<-as.numeric(nbes1996$QY3) 
nbes1996$education[nbes1996$education == 10] <- NA
nbes1996$education <- (nbes1996$education - 1)/8

nbes1996$polint[nbes1996$QA1 == "VERY INTERESTED"] <- 1
nbes1996$polint[nbes1996$QA1 == "SOMEWHAT INTERESTED"] <- 0.5
nbes1996$polint[nbes1996$QA1 == "NOT MUCH INTERESTED"] <- 0
nbes1996$polint[nbes1996$QA1 == "DK"] <- 0


nbes1996$relig[nbes1996$QU3 == "NEVER"] <- 0
nbes1996$relig[nbes1996$QU3 == "A FEW TIMES A YEAR"] <- 0.2
nbes1996$relig[nbes1996$QU3 == "ONCE OR TWICE A MONTH"] <- 0.4
nbes1996$relig[nbes1996$QU3 == "ALMOST EVERY WEEK"] <- 0.6
nbes1996$relig[nbes1996$QU3 == "EVERY WEEK"] <- 0.8
nbes1996$relig[nbes1996$QU3 == "VOL"] <- 1

nbes1996$homeowner <- 0
nbes1996$homeowner[nbes1996$QY10 == "OWN"] <- 1
nbes1996$ses <- (nbes1996$homeowner + nbes1996$education + nbes1996$inc ) /3 

##final dataset for 1996----
final1996 <- nbes1996 %>%
  dplyr::select(pol_part, pol_partstandard, rgc, inc, age, female, education, polint, 
                relig, turnout, donate, ses, linkb)

final1996 <- final1996 %>%
  rename(edu = education,
         campact = pol_part,
         campactstandard = pol_partstandard,
         blf = linkb)

final1996$year <- rep("1996", 1216)

#American National Election Study 2004----
###anes2004###

table(anes2004$V043299) #race of respondent variable 
anes2004$race <- as.numeric(anes2004$V043299)
anes2004$black <- ifelse(anes2004$race == 1, 1, 0)

#subset to only black respondents
anes2004 <- anes2004 %>%
  filter(black == 1)

##political participation----
table(anes2004$V045014)#did r give money to a candidate
table(anes2004$V045015) #did r give money to a political party
table(anes2004$V045016) #did r give to any other group that supported/opposed candidate

#donate
anes2004 <- anes2004 %>%
  mutate(money_candidate = as.numeric(V045014), #make donate to a candidate numeric
         money_candidate = if_else(money_candidate == 2, 0, 1), #if no 0, if yes 1.
         money_party = as.numeric(V045015), #make donate to a  party numeric
         money_party = if_else(money_party == 2, 0, 1), #if no 0, if yes 1
         money_opponent = as.numeric(V045016), #make any other donation to group that supported/opposed candidate numeric
         money_opponent = if_else(money_opponent == 2, 0, 1), #if no 0, if yes 1
         cmoneysc = (money_candidate + money_party + money_opponent)/3 #combine donation variables and range from 0 to 1
  )

#work 
table(anes2004$V045010) #try to influence vote of others
table(anes2004$V045011) #go to campaign, meetings, rallies, speeches
table(anes2004$V045012) #display campaign button/sticker
table(anes2004$V045013) #do any other campaign work

anes2004 <- anes2004 %>%
  mutate(camp = as.numeric(V045010), #make numeric
         camp = if_else(camp == 2, 0, 1), #if no 0, if yes 1 
         rally = as.numeric(V045011), #make numeric
         rally = if_else(rally == 2, 0, 1), #if no 0, if yes 1
         sign = as.numeric(V045012), #make numeric
         sign = if_else(sign == 2, 0, 1), #if no 0, if yes 1  
         oth = as.numeric(V045013), #make numeric
         oth = if_else(oth == 2, 0, 1), #if no 0, if yes 1
         campwsc = (camp + rally + sign + oth)/4 #combine campaign variables
  )

#turnout

table(anes2004$V045018X) #turnout/registration

anes2004 <- anes2004 %>%
  mutate(turnout = as.numeric(V045018X),
         turnout = if_else(turnout == 1, 1, 0)) #if stated voter, then 1, else 0

#big participation scale
anes2004$campact = (anes2004$money_candidate + anes2004$money_party + anes2004$money_opponent + 
                      anes2004$sign + anes2004$camp + anes2004$turnout + anes2004$rally + anes2004$oth)/8 #make scale and range from 0 to 1
#standardize by dividing the proportion of campaign activities by the standard deviation
anes2004$pol_partstandard <- (anes2004$campact - mean(anes2004$campact, na.rm = TRUE)) / 
  (sd(anes2004$campact, na.rm = TRUE))


##rgc------

#black linked fate

table(anes2004$V045177) #do you believe fate of blacks linked
table(anes2004$V045177A) #how much do you believe fate of blacks linked

anes2004 <- anes2004 %>%
  mutate(lf1 = as.numeric(V045177), #make variables numeic
         lf2 = as.numeric(V045177A),
         linkb = case_when(
           lf1 == 3 ~ 0,#if no linked fate then 0
           lf1 == 4 ~ .33, #if not very much at all linked fate then .33
           lf2 == 3 ~ .33, 
           lf2 == 2 ~ .66, #if some linked fate then .66
           lf2 == 1 ~ 1 #if a lot of lniked fate then 1
         )
  )

#black white feeling thermometer

table(anes2004$V045077) #black feeling thermomemter
table(anes2004$V045086) #white feeling thermometer
#none of these variables had 777. Don't recognize, 888. Don't know where to rate, 889. Refused

anes2004$ftb <- anes2004$V045077/100
anes2004$ftw <- anes2004$V045086/100

anes2004$bmw = anes2004$ftb - anes2004$ftw

#discrimination 

table(anes2004$V045194) #History makes more difficult for blacks to succeed
anes2004$disc <- as.numeric(anes2004$V045194) #make variable numeric
anes2004$disc <- 5 - anes2004$disc #reverse code and range from 0 to 4
anes2004$disc <- anes2004$disc/4 #range from 0 to 1


table(anes2004$V043159) #importance of aid to blacks
anes2004$impb <- as.numeric(anes2004$V043159) #make variable numeric
anes2004$impb <- 5 - anes2004$impb #reverse code and range from 0 to 4
anes2004$impb <- anes2004$impb/4 #range from 0 to 1

#rgc general variable
anes2004$rgc <- (rowMeans(anes2004[c("disc", "impb", "ftb", "linkb")], na.rm=TRUE))


#make separate datasets for rgc and political participation to get cronbach alpha
d <- anes2004
d <- d %>%
  dplyr::select(disc, impb, ftb, linkb)

d2 <- anes2004
d2 <- d2 %>%
  dplyr::select(money_candidate, money_party, money_opponent, 
                sign, camp, turnout, rally, oth)

#see cronbach alpha of rgc and political participation variable
cronbach.alpha(d, na.rm = TRUE)

cronbach.alpha(d2, na.rm = TRUE)

##demographics----

anes2004$age<-anes2004$V043250

anes2004$income<-as.numeric(anes2004$V043294)
anes2004$income <- anes2004$income/23

anes2004$education<-as.numeric(anes2004$V043254)
anes2004$education <- (anes2004$education-2)/6

anes2004$polint[anes2004$V043001 == "1. Very much interested"] <-1
anes2004$polint[anes2004$V043001 == "3. Somewhat interested"] <-0.5
anes2004$polint[anes2004$V043001 == "5. Not much interested"] <-0

anes2004$homeowner[anes2004$V043312 == "1. Own house"] <- 1
anes2004$homeowner[anes2004$V043312 != "1. Own house"] <- 0

anes2004$ses <- (anes2004$homeowner + anes2004$education + anes2004$income ) / 3

anes2004$female <- 0
anes2004$female[anes2004$V041109A == "2. Female"] <- 1

anes2004$relig[anes2004$V043223 == "5. No"] <- 0
anes2004$relig[anes2004$V043224 == "1. Every week"] <- 1
anes2004$relig[anes2004$V043224 == "2. Almost every week"] <- 0.8
anes2004$relig[anes2004$V043224 == "3. Once or twice a month"] <- 0.6
anes2004$relig[anes2004$V043224 == "4. A few times a year"] <- 0.4
anes2004$relig[anes2004$V043224 == "5. Never"] <- 0.2

##final dataset for 2004----
final2004 <- anes2004 %>%
  dplyr::select(pol_partstandard, campact, rgc, age, income, education, polint, female,
                relig, money_candidate, turnout, linkb, ses)
final2004 <- final2004 %>%
  rename(campactstandard = pol_partstandard,
         inc = income,
         edu = education,
         donate = money_candidate,
         blf = linkb)

final2004$year <- rep("2004", 180)

#ANES 2008----

#creating vote variable
anes2008$vote <- NA
anes2008$vote[anes2008$V085036X == "0. Did not vote in November 2008"] <- 0
anes2008$vote[anes2008$V085036X == "1. Voted in November 2008"] <- 1

#recoding political attention#
anes2008$att <- NA
anes2008$att[anes2008$V085023 == "1. A great deal"] <- 1
anes2008$att[anes2008$V085023 == "2. A lot"] <- .75
anes2008$att[anes2008$V085023 == "3. A moderate amount"] <- .50
anes2008$att[anes2008$V085023 == "4. A little"] <- .25
anes2008$att[anes2008$V085023 == "5. None at all"] <- .0

anes2008$att1 <- NA
anes2008$att1[anes2008$V083001a == "1. Very much interested"] <- 1
anes2008$att1[anes2008$V083001a == "3. Somewhat interested"] <- .5
anes2008$att1[anes2008$V083001a == "5. Not much interested"] <- 0
anes2008$att1[anes2008$V083001a == "-1. INAP, R selected for VERSION B " & anes2008$V083001b == "1. Extremely interested"] <- 1
anes2008$att1[anes2008$V083001a == "-1. INAP, R selected for VERSION B " & anes2008$V083001b == "2. Very interested"] <- .75
anes2008$att1[anes2008$V083001a == "-1. INAP, R selected for VERSION B " & anes2008$V083001b == "3. Moderately interested"] <- .50
anes2008$att1[anes2008$V083001a == "-1. INAP, R selected for VERSION B " & anes2008$V083001b == "4. Slightly interested"] <- .25
anes2008$att1[anes2008$V083001a == "-1. INAP, R selected for VERSION B " & anes2008$V083001b == "5. Not interested at all"] <- 0

anes2008$attsc <- (anes2008$att1+anes2008$att) / 2

###recode income###
anes2008$inc <- as.numeric(anes2008$V083248X)
anes2008$inc[anes2008$inc <= 2] <- NA
anes2008$inc <- (anes2008$inc - 3) / 24

###recode education###
anes2008$education <- as.numeric(anes2008$V083218X)
anes2008$education <- (anes2008$education - 4) / 6
anes2008$education[anes2008$education < 0] <- NA

#age
anes2008$age <- anes2008$V081104
anes2008$age[anes2008$age < 17] <- NA

#sex
anes2008$sex <- as.numeric(anes2008$V081101)

###ftblack###
anes2008$ftb<- anes2008$V085064Y
anes2008$ftb[anes2008$ftb < 0] <- NA

##feeling therm black 0-1##
anes2008$ftb1 = anes2008$ftb / 100

##black influence/power##
anes2008$blackp <- NA
anes2008$blackp[anes2008$V085114 == "1. Too much influence"] <- 0
anes2008$blackp[anes2008$V085114 == "3. Just about the right amount of influence"] <- 0.5
anes2008$blackp[anes2008$V085114 == "5. Too little influence"] <- 1

###black importance, aid to black##
anes2008$impb <- NA
anes2008$impb[anes2008$V083138B == "1. Not important at all"] <- 1
anes2008$impb[anes2008$V083138B == "2. Slightly important"] <- 2
anes2008$impb[anes2008$V083138B == "3. Moderately important"] <- 3
anes2008$impb[anes2008$V083138B == "4. Very important"] <- 4
anes2008$impb[anes2008$V083138B == "5. Extremely important"] <- 5

###discrimination###
anes2008$disc <- NA
anes2008$disc[anes2008$V085144 == "1. Agree strongly"] <- 1
anes2008$disc[anes2008$V085144 == "2. Agree Somewhat"] <- .75
anes2008$disc[anes2008$V085144 == "3. Neither agree nor disagree"] <- .5
anes2008$disc[anes2008$V085144 == "4. Disagree Somewhat"] <- .25
anes2008$disc[anes2008$V085144 == "5. Disagree strongly"] <- 0

####felt admiration##
anes2008$admblack <- NA
anes2008$admblack[anes2008$V085116 == "1. Very often"] <- 1
anes2008$admblack[anes2008$V085116 == "2. Fairly often"] <- .66
anes2008$admblack[anes2008$V085116 == "3. Not too often"] <- .33
anes2008$admblack[anes2008$V085116 == "4. Never"] <- 0


###money##
anes2008$money<- NA
anes2008$money[anes2008$V085033 == "1. Yes"]<- 1
anes2008$money[anes2008$V085033 == "5. No"]<- 0

anes2008$money1 <- NA
anes2008$money1[anes2008$V085034 == "1. Yes"] <- 1
anes2008$money1[anes2008$V085034 == "5. No"] <- 0

anes2008$moneyo <- NA
anes2008$moneyo[anes2008$V085035 == "1. Yes"] <- 1
anes2008$moneyo[anes2008$V085035 == "5. No"] <- 0


####campaign work###
anes2008$camp <-NA
anes2008$camp[anes2008$V085029 == "1. Yes"] <-1
anes2008$camp[anes2008$V085029 == "5. No"] <-0

##rally##
anes2008$rally <- NA
anes2008$rally[anes2008$V085030 == "1. Yes"]<-1
anes2008$rally[anes2008$V085030 == "5. No"]<-0

#sign
anes2008$sign <- NA
anes2008$sign[anes2008$V085031 == "1. Yes"]<-1
anes2008$sign[anes2008$V085031 == "5. No"]<-0

#other#
anes2008$oth <- NA
anes2008$oth[anes2008$V085032 == "1. Yes"]<-1
anes2008$oth[anes2008$V085032 == "5. No"]<-0


###linkb###
anes2008$linkb <- NA
anes2008$linkb[anes2008$V085133 == "1. Yes"] <- 1
anes2008$linkb[anes2008$V085133 == "5. No"] <- 0
anes2008$linkb[anes2008$V085133 == "7. Depends {SPECIFY}"] <- 0.33

anes2008$linkb[anes2008$linkb == 1 & anes2008$V085133A == "3. Some"] <- 0.66
anes2008$linkb[anes2008$linkb == 1 & anes2008$V085133A == "5. Not very much at all"] <- 0.33


#impr
anes2008$impr <- NA
anes2008$impr[anes2008$V083138A == "1. Not important at all"] <- 1
anes2008$impr[anes2008$V083138A == "2. Not too important"] <- 2
anes2008$impr[anes2008$V083138A == "3. Somewhat important"] <- 3
anes2008$impr[anes2008$V083138A == "4. Very important"] <- 4
anes2008$impr[anes2008$V083138A == "5. Extremely important"] <- 5

anes2008$impr[anes2008$V083138A == "-1. INAP, R selected for VERSION NEW" & anes2008$V083138B == "1. Not important at all"] <- 1
anes2008$impr[anes2008$V083138A == "-1. INAP, R selected for VERSION NEW" & anes2008$V083138B == "2. Not too important"] <- 2
anes2008$impr[anes2008$V083138A == "-1. INAP, R selected for VERSION NEW" & anes2008$V083138B == "3. Somewhat important"] <- 3
anes2008$impr[anes2008$V083138A == "-1. INAP, R selected for VERSION NEW" & anes2008$V083138B == "4. Very important"] <- 4
anes2008$impr[anes2008$V083138A == "-1. INAP, R selected for VERSION NEW" & anes2008$V083138B == "5. Extremely important"] <- 5


##discg##
anes2008$discg <- NA
anes2008$discg[anes2008$V085144 == "1. Agree strongly"] <- 1
anes2008$discg[anes2008$V085144 == "2. Agree Somewhat"] <- 0.75
anes2008$discg[anes2008$V085144 == "3. Neither agree nor disagree"] <- 0.50
anes2008$discg[anes2008$V085144 == "4. Disagree Somewhat"] <- 0.25
anes2008$discg[anes2008$V085144 == "5. Disagree strongly"] <- 0.00


###RGC variables###
anes2008$blf <- anes2008$linkb
anes2008$impr1 <- (anes2008$impr-1)/4

###rowmeans###
anes2008$rgcmean <- (rowMeans(anes2008[c("discg", "impr1", "blf", "blackp", "ftb1", "admblack")], na.rm=TRUE))
##subset
anes2008$black <- 0
anes2008$black[anes2008$V081102 == "2. Black/African-American"] <- 1
anes2008$black[anes2008$V081102 == "6. Black and another race"] <- 1
anes2008$black[anes2008$V081102 == "7. White, black and another race"] <- 1

anesblackonly <- subset(anes2008, black == 1)

summary(anesblackonly$rgcmean)

##campact

anesblackonly$campact1 <- (anesblackonly$money+anesblackonly$money1+anesblackonly$moneyo+
                             anesblackonly$sign+anesblackonly$camp+anesblackonly$vote+anesblackonly$rally+
                             anesblackonly$oth)/8
summary(anesblackonly$campact1)


anesblackonly$female <- 0 
anesblackonly$female[anesblackonly$V081101 == "2. Female respondent selected"] <- 1

anesblackonly$polint[anesblackonly$V083001A == "1. Very much interested"| anesblackonly$V083001B == "1. Extremely interested"] <- 1
anesblackonly$polint[anesblackonly$V083001B == "2. Very interested"] <- 0.75
anesblackonly$polint[anesblackonly$V083001A == "3. Somewhat interested"| anesblackonly$V083001B == "3. Moderately interested"] <- 0.5
anesblackonly$polint[anesblackonly$V083001B == "4. Slightly interested"] <- 0.25
anesblackonly$polint[anesblackonly$V083001A == "5. Not much interested"| anesblackonly$V083001B == "5. Not interested at all"] <- 0

anesblackonly$religous[anesblackonly$V083186 == "5. No"] <- 0
anesblackonly$religous[anesblackonly$V083186A == "5. Never"] <- 0.2
anesblackonly$religous[anesblackonly$V083186A == "4. A few times a year"] <- 0.4
anesblackonly$religous[anesblackonly$V083186A == "3. Once or twice a month"] <- 0.6
anesblackonly$religous[anesblackonly$V083186A == "2. Almost every week"] <- 0.8
anesblackonly$religous[anesblackonly$V083186A == "1. Every week"] <- 1


#standardize by dividing the proportion of campaign activities by the standard deviation
anesblackonly$pol_partstandard <- (anesblackonly$campact1 - mean(anesblackonly$campact1, na.rm = TRUE)) / (sd(anesblackonly$campact1, na.rm = TRUE))

anesblackonly$homeowner <- 0
anesblackonly$homeowner[anesblackonly$V083281 == "1. Own home"] <- 1

anesblackonly$ses <- (anesblackonly$homeowner + anesblackonly$inc + anesblackonly$education) / 3


final2008 <- anesblackonly %>%
  dplyr::select(pol_partstandard, campact1, rgcmean, age, inc, education, female,
                polint, religous, vote, money, ses, blf)
final2008 <- final2008 %>%
  rename(campactstandard = pol_partstandard,
         campact = campact1,
         rgc = rgcmean,
         edu = education,
         relig = religous,
         turnout = vote,
         donate = money)

final2008$year <- rep("2008", 591)
#ANES2012----

#subset to only black respondents
anes2012black <- subset(anes2012, DEM_RACECPS_BLACK == "1. Selected by R")


###attention###
anes2012black$att <- NA
anes2012black$att[anes2012black$INTEREST_ATTENTION == "1. Always"] <- 1
anes2012black$att[anes2012black$INTEREST_ATTENTION == "2. Most of the time"] <- .75
anes2012black$att[anes2012black$INTEREST_ATTENTION == "3. About half the time"] <- .5
anes2012black$att[anes2012black$INTEREST_ATTENTION == "4. Some of the time"] <- .25
anes2012black$att[anes2012black$INTEREST_ATTENTION == "5. Never"] <- 0

anes2012black$att1 <- NA
anes2012black$att1[anes2012black$INTEREST_FOLLOWING == "1. Very much interested"] <- 1
anes2012black$att1[anes2012black$INTEREST_FOLLOWING == "2. Somewhat interested"] <- .5
anes2012black$att1[anes2012black$INTEREST_FOLLOWING == "3. Not much interested"] <- 0

anes2012black$attsc <- (anes2012black$att + anes2012black$att1) / 2

###income##
anes2012black$inc <- as.numeric(anes2012black$INCGROUP_PREPOST_X)
anes2012black$inc[anes2012black$inc <= 4] <- NA
anes2012black$inc <- anes2012black$inc - 4
anes2012black$inc <- anes2012black$inc / 28

###educ##
anes2012black$edu<-as.numeric(anes2012black$DEM_EDUGROUP_X)
anes2012black$edu[anes2012black$edu <= 3 ] <- NA
anes2012black$edu[anes2012black$edu == 4 ] <- 0
anes2012black$edu[anes2012black$edu == 5 ] <- .25
anes2012black$edu[anes2012black$edu == 6 ] <- .5
anes2012black$edu[anes2012black$edu == 7 ] <- .75
anes2012black$edu[anes2012black$edu == 8 ] <- 1

##age##
anes2012black$age <- anes2012black$DEM_AGE_R_X
anes2012black$age[anes2012black$age == -2] <- NA

###sex##
anes2012black$sex <- 0
anes2012black$sex[anes2012black$GENDER_RESPONDENT_X == "2. Female"] <- 1


###importance of being black to your identity###
anes2012black$bimp<-as.numeric(anes2012black$IDENT_BLACKID) 
anes2012black$bimp[anes2012black$bimp <= 6] <- NA 
anes2012black$bimp[anes2012black$bimp == 7] <- 1
anes2012black$bimp[anes2012black$bimp == 8] <- .75
anes2012black$bimp[anes2012black$bimp == 9] <- .5
anes2012black$bimp[anes2012black$bimp == 10] <- .25
anes2012black$bimp[anes2012black$bimp == 11] <- 0

#admiration for being black#
anes2012black$badm <- as.numeric(anes2012black$RACECASI_ADMBLACKS) 
anes2012black$badm[anes2012black$badm <= 4] <- NA 
anes2012black$badm[anes2012black$badm == 5] <- 1 
anes2012black$badm[anes2012black$badm == 6] <- .75
anes2012black$badm[anes2012black$badm == 7] <- .5
anes2012black$badm[anes2012black$badm == 8] <- .25
anes2012black$badm[anes2012black$badm == 9] <- 0

####feeling therm black###
anes2012black$ftb<-anes2012black$FTCASI_BLACK
anes2012black$ftb[anes2012black$ftb < 0] <- NA

###ftb1##
anes2012black$ftb1 <- anes2012black$ftb / 100

##power/influence##
anes2012black$blackp <- as.numeric(anes2012black$RACECASI_INFBLACKS)
anes2012black$blackp[anes2012black$blackp <=4] <- NA
anes2012black$blackp[anes2012black$blackp ==5] <- 0
anes2012black$blackp[anes2012black$blackp ==6] <- .5
anes2012black$blackp[anes2012black$blackp ==7] <- 1

###discrimination##
table(anes2012black$DISCRIM_BLACKS)
anes2012black$discb <- as.numeric(anes2012black$DISCRIM_BLACKS)
anes2012black$discb[anes2012black$discb <=4] <- NA
anes2012black$discb[anes2012black$discb == 5] <- 1
anes2012black$discb[anes2012black$discb == 6] <- 0.75
anes2012black$discb[anes2012black$discb == 7] <- 0.5
anes2012black$discb[anes2012black$discb == 8] <- 0.25
anes2012black$discb[anes2012black$discb == 9] <- 0

###racial resentment discrimination###
table(anes2012black$RESENT_SLAVERY)
anes2012black$rrdisc <- as.numeric(anes2012black$RESENT_SLAVERY)
anes2012black$rrdisc[anes2012black$rrdisc <=4] <- NA
anes2012black$rrdisc[anes2012black$rrdisc == 5] <- 1
anes2012black$rrdisc[anes2012black$rrdisc == 6] <- 0.75
anes2012black$rrdisc[anes2012black$rrdisc == 7] <- 0.5
anes2012black$rrdisc[anes2012black$rrdisc == 8] <- 0.25
anes2012black$rrdisc[anes2012black$rrdisc == 9] <- 0

#Do you think that what happens to
#Black People in this country will have something to do with what happens in your life?
#linkfate#
anes2012black$linkfate <- NA 
anes2012black$linkfate[anes2012black$LINK_BLACK == "1. Yes" & anes2012black$LINK_BLACKAMT == "1. A lot"] <- 1
anes2012black$linkfate[anes2012black$LINK_BLACK == "1. Yes" & anes2012black$LINK_BLACKAMT == "2. Some"] <- .66
anes2012black$linkfate[anes2012black$LINK_BLACK == "1. Yes" & anes2012black$LINK_BLACKAMT == "3. Not very much"] <- .33
anes2012black$linkfate[anes2012black$LINK_BLACK == "2. No"] <- 0


##racial group consciousness----

anes2012black$rgcmean <- (rowMeans(anes2012black[c("linkfate", "rrdisc", "discb", "blackp", "ftb1", 
                                                   "bimp", "badm")], na.rm=TRUE))

summary(anes2012black$rgcmean)

#cronbach alpha rgc
myvarsrgc <- c("linkfate", "rrdisc", "discb", "blackp", "ftb1", "bimp", "badm")
ndrgc <- anes2012black[myvarsrgc]

cronbach.alpha(ndrgc, na.rm = TRUE)

##participation scale----

##vote###
anes2012black$vote2012 <- NA
anes2012black$vote2012[anes2012black$RVOTE2012_X == "1. R voted in the 2012 elections"] <- 1
anes2012black$vote2012[anes2012black$RVOTE2012_X == "2. R did not vote in the 2012 elections"] <- 0

#money individual candidate#
table(anes2012black$MOBILPO_CTBCAND)
anes2012black$canddonate<-as.numeric(anes2012black$MOBILPO_CTBCAND)
anes2012black$canddonate[anes2012black$canddonate <= 4] <- NA
anes2012black$canddonate[anes2012black$canddonate <= 5] <- 1
anes2012black$canddonate[anes2012black$canddonate == 6] <- 0

#money political party this year#
table(anes2012black$MOBILPO_CTBPTY)
anes2012black$partydonate<-as.numeric(anes2012black$MOBILPO_CTBPTY)
anes2012black$partydonate[anes2012black$partydonate <= 4] <- NA
anes2012black$partydonate[anes2012black$partydonate == 5] <- 1
anes2012black$partydonate[anes2012black$partydonate == 6] <- 0

##give money to any other group?##
table(anes2012black$MOBILPO_CTBOTH)
anes2012black$donateoth<-as.numeric(anes2012black$MOBILPO_CTBOTH)
anes2012black$donateoth[anes2012black$donateoth <= 4] <- NA
anes2012black$donateoth[anes2012black$donateoth == 5] <- 1
anes2012black$donateoth[anes2012black$donateoth == 6] <- 0


###social or political org giving##
#do we want to include this one#?
table(anes2012black$DHSINVOLV_ORG)
anes2012black$donatesocpol<-as.numeric(anes2012black$DHSINVOLV_ORG)
anes2012black$donatesocpol[anes2012black$donatesocpol <= 4] <- NA
anes2012black$donatesocpol[anes2012black$donatesocpol == 5] <- 1
anes2012black$donatesocpol[anes2012black$donatesocpol == 6] <- 0

##work##
#sign#
table(anes2012black$MOBILPO_SIGN)
anes2012black$signs<-as.numeric(anes2012black$MOBILPO_SIGN)
anes2012black$signs[anes2012black$signs <= 4] <- NA
anes2012black$signs[anes2012black$signs == 5] <- 1
anes2012black$signs[anes2012black$signs == 6] <- 0

#othwork#
table(anes2012black$MOBILPO_OTHERWORK)
anes2012black$othwork<-as.numeric(anes2012black$MOBILPO_OTHERWORK)
anes2012black$othwork[anes2012black$othwork <= 4] <- NA
anes2012black$othwork[anes2012black$othwork == 5] <- 1
anes2012black$othwork[anes2012black$othwork == 6] <- 0

#rally#
table(anes2012black$MOBILPO_RALLY)
anes2012black$rally<-as.numeric(anes2012black$MOBILPO_RALLY)
anes2012black$rally[anes2012black$rally <= 4] <- NA
anes2012black$rally[anes2012black$rally == 5] <- 1
anes2012black$rally[anes2012black$rally == 6] <- 0

#convince
table(anes2012black$MOBILPO_RMOB)
anes2012black$convince<-as.numeric(anes2012black$MOBILPO_RMOB)
anes2012black$convince[anes2012black$convince <= 4] <- NA
anes2012black$convince[anes2012black$convince == 5] <- 1
anes2012black$convince[anes2012black$convince == 6] <- 0

#mean#
anes2012black$partscale <- (anes2012black$rally + anes2012black$convince + anes2012black$signs + anes2012black$othwork  
                            + anes2012black$vote2012 + anes2012black$canddonate + anes2012black$partydonate 
                            + anes2012black$donateoth)  
summary(anes2012black$partscale)
##cronbach alpha part##
myvarspart <- c("rally", "signs", "othwork", "convince", "vote2012", "canddonate", "partydonate", "donateoth")
ndpart <- anes2012black[myvarspart]

cronbach.alpha(ndpart, na.rm = TRUE)

anes2012black$campact1=(anes2012black$rally + anes2012black$convince + anes2012black$signs + anes2012black$othwork  
                        + anes2012black$vote2012 + anes2012black$canddonate + anes2012black$partydonate 
                        + anes2012black$donateoth)/8

anes2012black$religious[anes2012black$RELIG_CHURCH == "2. No"] <- 0
anes2012black$religious[anes2012black$RELIG_CHURCHOFT == "1. Every week"] <- 1
anes2012black$religious[anes2012black$RELIG_CHURCHOFT == "2. Almost every week"] <- .8
anes2012black$religious[anes2012black$RELIG_CHURCHOFT == "3. Once or twice a month"] <- .6
anes2012black$religious[anes2012black$RELIG_CHURCHOFT == "4. A few times a year"] <- .4
anes2012black$religious[anes2012black$RELIG_CHURCHOFT == "5. Never"] <- .2


#standardize by dividing the proportion of campaign activities by the standard deviation
anes2012black$campactstandard <- (anes2012black$campact1 - (mean(anes2012black$campact1, na.rm = TRUE))) / (sd(anes2012black$campact1, na.rm = TRUE))

anes2012black$homeowner <- 0
anes2012black$homeowner[anes2012black$DEM3_OWNHOME == "1. Own home"] <- 1

anes2012black$ses <- (anes2012black$homeowner + anes2012black$inc + anes2012black$edu) / 3


final2012 <- anes2012black %>%
  dplyr::select(campactstandard, campact1, edu, age, sex, attsc, inc, rgcmean, religious,
                vote2012, canddonate, ses, linkfate)

final2012 <- final2012 %>%
  rename(campact = campact1,
         female = sex,
         polint = attsc,
         rgc = rgcmean,
         relig = religious,
         turnout = vote2012,
         donate = canddonate,
         blf = linkfate)

final2012$year <- rep("2012", 1132)
#ANES 2016----

anes2016$V161310x <- as.numeric(anes2016$V161310X)
anes2016 <- anes2016[anes2016$V161310x == 3, ]

####political participation----
#turnout

table(anes2016$V162031X)
anes2016$vote <- as.numeric(anes2016$V162031X)
anes2016$vote[anes2016$vote == 2] <- NA #make don't know NA
anes2016$vote[anes2016$vote == 5] <- NA #make NA, NA
anes2016$vote[anes2016$vote == 6] <- 0 #make did not vote in2016 0
anes2016$vote[anes2016$vote == 7] <- 1 #make voted in 2016 1

#give money to a political party
table(anes2016$V162016)

anes2016$moneyp <- as.numeric(anes2016$V162016)
anes2016$moneyp[anes2016$moneyp < 5] <- NA #make don't know NA
anes2016$moneyp[anes2016$moneyp == 5] <- 1 #make did donate to political party 1
anes2016$moneyp[anes2016$moneyp == 6] <- 0 #make did not donate to political party 0

#give money to political candidate/campaign
table(anes2016$V162014)
anes2016$moneyc <- as.numeric(anes2016$V162014)
anes2016$moneyc[anes2016$moneyc < 5] <- NA #make don't know NA
anes2016$moneyc[anes2016$moneyc == 5] <- 1 #make did donate to candidate 1
anes2016$moneyc[anes2016$moneyc == 6] <- 0 #make did not donate to candidate 0

#donate to any other group that supported or opposed candidate
table(anes2016$V162017)
anes2016$moneyo <- as.numeric(anes2016$V162017)
anes2016$moneyo[anes2016$moneyo <= 3] <- NA #make don't know NA
anes2016$moneyo[anes2016$moneyo == 4] <- 1 #make did donate to candidate 1
anes2016$moneyo[anes2016$moneyo == 5] <- 0 #make did not donate to candidate 0

#talk to anyone about voting for or against a candidate
table(anes2016$V162010)
anes2016$talk <- as.numeric(anes2016$V162010)
anes2016$talk[anes2016$talk <= 3] <- NA #make don't know NA
anes2016$talk[anes2016$talk == 4] <- 1 #make did donate to candidate 1
anes2016$talk[anes2016$talk == 5] <- 0 #make did not donate to candidate 0

#rally
table(anes2016$V162011)
anes2016$rally <- as.numeric(anes2016$V162011)
anes2016$rally[anes2016$rally <= 3] <- NA #make don't know NA
anes2016$rally[anes2016$rally == 4] <- 1 #make did donate to candidate 1
anes2016$rally[anes2016$rally == 5] <- 0 #make did not donate to candidate 0

#sign
table(anes2016$V162012)
anes2016$sign <- as.numeric(anes2016$V162012)
anes2016$sign[anes2016$sign <= 3] <- NA #make don't know NA
anes2016$sign[anes2016$sign == 4] <- 1 #make did donate to candidate 1
anes2016$sign[anes2016$sign == 5] <- 0 #make did not donate to candidate 0

#other
table(anes2016$V162013) #do any other work for campaign/candidate
anes2016$oth <- as.numeric(anes2016$V162013)
anes2016$oth[anes2016$oth <= 3] <- NA #make don't know NA
anes2016$oth[anes2016$oth == 4] <- 1 #make did donate to candidate 1
anes2016$oth[anes2016$oth == 5] <- 0 #make did not donate to candidate 0

#make political participation variable
d2 <- anes2016
d2 <- d2 %>%
  dplyr::select(vote, moneyp, moneyc, moneyo, talk, sign, oth, rally)

cronbach.alpha(d2, na.rm = TRUE)

anes2016$pol_part <- (d2$vote + d2$moneyp + d2$moneyc + d2$moneyo + d2$talk + d2$sign + 
                  d2$oth + d2$rally)/8

####rgc----

#black importance
table(anes2016$V162328) #how important is being black to your identity
anes2016$bimp <- as.numeric(anes2016$V162328)
anes2016$bimp[anes2016$bimp <= 5] <- NA #make don't know NA
anes2016$bimp <- anes2016$bimp - 6 #start at 0
anes2016$bimp <- 4 - anes2016$bimp #reverse code so extremely important is higher
anes2016$bimp <- anes2016$bimp/4 #range from 0 to 1

#black feeling thermometer
table(anes2016$V162312)
anes2016$ftb <- anes2016$V162312
anes2016$ftb[anes2016$ftb < 0] <- NA #make don't know NA
anes2016$ftb <- anes2016$ftb/100

#black influence
table(anes2016$V162323)#how much influence do black ameericans have in politics
anes2016$binf <- as.numeric(anes2016$V162323)
anes2016$binf[anes2016$binf <= 4] <- NA #make don't know NA
anes2016$binf <- anes2016$binf - 5 #start at 0
anes2016$binf <- anes2016$binf/2 #range from 0 to 1

#black discrimination 
table(anes2016$V162357) #how much discrimination is there in the united states against blacks
anes2016$discb <- as.numeric(anes2016$V162357)
anes2016$discb[anes2016$discb <= 4] <- NA #make don't know NA
anes2016$discb <- anes2016$discb - 5 #start at 0 
anes2016$discb <- 4 - anes2016$discb #reverse code
anes2016$discb <- anes2016$discb/4 #range from 0 to 1

#generations of slavery...
table(anes2016$V162212)
anes2016$discg <- as.numeric(anes2016$V162212)
anes2016$discg[anes2016$discg <= 4] <- NA #make don't know NA
anes2016$discg <- anes2016$discg - 5 #start at 0 
anes2016$discg <- 4 - anes2016$discg #reverse code
anes2016$discg <- anes2016$discg/4

#black linked fate
table(anes2016$V162225)
anes2016$blf <- as.numeric(anes2016$V162225)
anes2016$blf[anes2016$blf <= 4] <- NA #make don't know NA
anes2016$blf <- anes2016$blf - 5 #start at 0 
anes2016$blf <- 3 - anes2016$blf #reverse code
anes2016$blf <- anes2016$blf/3 #range from 0 to 1


#make rgc variable
d <- anes2016
d <- d %>%
  dplyr::select(blf, discg, discb, binf, ftb, bimp)

cronbach.alpha(d, na.rm = TRUE)

anes2016$rgc <- rowMeans(anes2016[c("blf", "discg", "discb", "binf", "ftb", "bimp")], na.rm = TRUE)

####controls----


table(anes2016$V161361X) #income
anes2016$inc1 <- as.numeric(anes2016$V161361X)
anes2016$inc1[anes2016$inc1 == 1] <- NA #make don't know NA
anes2016$inc1[anes2016$inc1 == 2] <- NA #make don't know NA
anes2016$inc1 <- anes2016$inc1 - 3 #start at 0
anes2016$inc1 <- anes2016$inc1/27 #range from 0 to 1


anes2016$female <- 0 
anes2016$female[anes2016$V161342 == "2. Female"] <- 1

anes2016$polint[anes2016$V162256 == "1. Very interested"] <- 1
anes2016$polint[anes2016$V162256 == "2. Somewhat interested"] <- 0.66
anes2016$polint[anes2016$V162256 == "3. Not very interested"] <- 0.33
anes2016$polint[anes2016$V162256 == "4. Not at all interested"] <- 0

anes2016$education <- as.numeric(anes2016$V161270)
anes2016$education[anes2016$education <= 2] <- NA
anes2016$education[anes2016$education >= 19] <- NA
anes2016$edu <- (anes2016$education - 5)/13

anes2016$age <- anes2016$V161267

anes2016$religion[anes2016$V161244 == "2. No"] <- 0 
anes2016$religion[anes2016$V161245 == "5. Never"] <- 0.2
anes2016$religion[anes2016$V161245 == "4. A few times a year"] <- 0.4
anes2016$religion[anes2016$V161245 == "3. Once or twice a month"] <- 0.6
anes2016$religion[anes2016$V161245 == "2. Almost every week"] <- 0.8
anes2016$religion[anes2016$V161245 == "1. Every week"] <- 1

#standardize by dividing the proportion of campaign activities by the standard deviation
anes2016$pol_partstandard <- (anes2016$pol_part - mean(anes2016$pol_part, na.rm = TRUE))/ (sd(anes2016$pol_part, na.rm = TRUE))

anes2016$homeowner <- 0 
anes2016$homeowner[anes2016$V161334 == "2. Pay mortgage" | anes2016$V161334 == "3. Own home with no payments due"] <- 1

anes2016$ses <- (anes2016$edu + anes2016$inc1 + anes2016$homeowner) / 3

##final dataset for 2016----
final2016 <- anes2016 %>%
  dplyr::select(pol_part, pol_partstandard, rgc, inc1, female, polint, edu, age, 
                religion, vote, moneyc, ses, blf)

final2016 <- final2016 %>%
  rename(campact = pol_part,
         campactstandard = pol_partstandard,
         inc = inc1,
         relig = religion,
         turnout = vote,
         donate = moneyc)

final2016$year <- rep("2016", 398)

#ANES 2020----

#subset to only black respondents

d_20 <- subset(anes20, anes20$V201549x == 2)

####participation scale####

#turnout

table(d_20$V202072)
d_20$vote[d_20$V202072 == 2] <- 0#make did not vote in2020 0
d_20$vote[d_20$V202072 == 1] <- 1 #make voted in 2020 1
d_20$vote[d_20$V202072 == -7] <- NA #
d_20$vote[d_20$V202072 == -6] <- NA
d_20$vote[d_20$V202072 == -1] <- 0

#give money to a candidate
table(d_20$V202017)

d_20$moneyp[d_20$V202017 <= 0] <- NA #make don't know NA
d_20$moneyp[d_20$V202017 == 1] <- 1 #make did donate to political party 1
d_20$moneyp[d_20$V202017 == 2] <- 0 #make did not donate to political party 0

#give money to political party
table(d_20$V202019)
d_20$moneyc <- as.numeric(d_20$V202019)
d_20$moneyc[d_20$moneyc < 1] <- NA #make don't know NA
d_20$moneyc[d_20$moneyc == 1] <- 1 #make did donate to candidate 1
d_20$moneyc[d_20$moneyc == 2] <- 0 #make did not donate to candidate 0

#donate to any other group that supported or opposed candidate
table(d_20$V202021)
d_20$moneyo <- as.numeric(d_20$V202021)
d_20$moneyo[d_20$moneyo < 1] <- NA #make don't know NA
d_20$moneyo[d_20$moneyo == 1] <- 1 #make did donate to candidate 1
d_20$moneyo[d_20$moneyo == 2] <- 0 #make did not donate to candidate 0


#talk to anyone about voting for or against a candidate
table(d_20$V202009)
d_20$talk <- as.numeric(d_20$V202009)
d_20$talk[d_20$talk < 1] <- NA #make don't know NA
d_20$talk[d_20$talk == 2] <- 0 #make did not donate to candidate 0

#rally
table(d_20$V202014)
d_20$rally <- as.numeric(d_20$V202014)
d_20$rally[d_20$rally < 1] <- NA #make don't know NA
d_20$rally[d_20$rally == 2] <- 0 #make did not donate to candidate 0

#sign - wear campaign button, or post sign or bumper sticker
table(d_20$V202015)
d_20$sign <- as.numeric(d_20$V202015)
d_20$sign[d_20$sign < 1] <- NA #make don't know NA
d_20$sign[d_20$sign == 2] <- 0 #make did not donate to candidate 0

#other
table(d_20$V202016) #do any other work for campaign/candidate
d_20$oth <- as.numeric(d_20$V202016)
d_20$oth[d_20$oth < 1] <- NA #make don't know NA
d_20$oth[d_20$oth == 2] <- 0 #make did not donate to candidate 0

#make political participation variable
d2 <- d_20
d2 <- d2 %>%
  dplyr::select(vote, moneyp, moneyc, moneyo, talk, sign, oth, rally)

cronbach.alpha(d2, na.rm = TRUE)

d_20$pol_part <- (d_20$vote + d_20$moneyp + d_20$moneyc + d_20$moneyo + d_20$talk + d_20$sign + 
                  d_20$oth + d_20$rally)/8

####rgc----

#black importance
table(d_20$V202500x) #how important is being black to your identity
d_20$bimp <- as.numeric(d_20$V202500x)
d_20$bimp[d_20$bimp < 1] <- NA #make don't know NA
d_20$bimp <- d_20$bimp - 1 #start at 0
d_20$bimp <- 4 - d_20$bimp #reverse code so extremely important is higher
d_20$bimp <- d_20$bimp/4 #range from 0 to 1

#black feeling thermometer
table(d_20$V202480)
d_20$ftb <- d_20$V202480
d_20$ftb[d_20$ftb < 0] <- NA #make don't know NA
d_20$ftb <- d_20$ftb/100

#black influence
table(d_20$V202495)#how much influence do black ameericans have in politics
d_20$binf <- as.numeric(d_20$V202495)
d_20$binf[d_20$binf < 1] <- NA #make don't know NA
d_20$binf <- d_20$binf - 1 #start at 0
d_20$binf <- d_20$binf/2 #range from 0 to 1

#black discrimination 
table(d_20$V202527) #how much discrimination is there in the united states against blacks
d_20$discb <- as.numeric(d_20$V202527)
d_20$discb[d_20$discb < 1] <- NA #make don't know NA
d_20$discb <- d_20$discb - 1 #start at 0 
d_20$discb <- 4 - d_20$discb #reverse code
d_20$discb <- d_20$discb/4 #range from 0 to 1

#generations of slavery...
table(d_20$V202301)
d_20$discg <- as.numeric(d_20$V202301)
d_20$discg[d_20$discg < 1] <- NA #make don't know NA
d_20$discg <- 5 - d_20$discg #reverse code
d_20$discg <- d_20$discg/4

#black linked fate
table(d_20$V202507)
d_20$blf <- as.numeric(d_20$V202507)
d_20$blf[d_20$blf < 1] <- NA #make don't know NA
d_20$blf <- d_20$blf - 1 #start at 0 
d_20$blf <- 3 - d_20$blf #reverse code
d_20$blf <- d_20$blf/3 #range from 0 to 1


#make rgc variable
d <- d_20
d <- d %>%
  dplyr::select(blf, discg, discb, binf, ftb, bimp)

cronbach.alpha(d, na.rm = TRUE)

d_20$rgc <- rowMeans(d_20[c("blf", "discg", "discb", "binf", "ftb", "bimp")], na.rm = TRUE)

####demographics####
d_20$inc1 <- d_20$V201617x
d_20$inc1[d_20$inc1 < 1] <- NA #make don't know NA
d_20$inc1 <- d_20$inc1 - 1
d_20$inc1 <- d_20$inc1/21

#female
table(d_20$V201600)
d_20$female <- d_20$V201600
d_20$female[d_20$female < 1] <- NA #make don't know NA
d_20$female <- d_20$female - 1 #female is 1, male is 0

#education
table(d_20$V201510)
d_20$edu <- d_20$V201510
d_20$edu[d_20$edu < 1] <- NA #make don't know NA
d_20$edu[d_20$edu > 8] <- NA #make don't know NA
d_20$edu <- d_20$edu - 1
d_20$edu <- d_20$edu/7

#age
table(d_20$V201507x)
d_20$age <- d_20$V201507x
d_20$age[d_20$age < 18] <- NA

#religiosity
table(d_20$V201433) #how important is religion in your life
d_20$religion <- d_20$V201433
d_20$religion[d_20$religion < 1] <- NA
d_20$religion <- d_20$religion -1 #START AT 0
d_20$religion <- 4 - d_20$religion #reverse code
d_20$religion <- d_20$religion/4

table(d_20$V201006) #Some people don’t pay much attention to political campaigns. How about you? 
d_20$polint <- d_20$V201006
d_20$polint <- d_20$polint - 1 #start at 0
d_20$polint <- 2 - d_20$polint #reverse code
d_20$polint <- d_20$polint/2



#standardizing dv
d_20$pol_partstandard <- (d_20$pol_part - mean(d_20$pol_part, na.rm = TRUE))/ (sd(d_20$pol_part, na.rm = TRUE))


d_20$homeowner <- 0
d_20$homeowner[d_20$V201581 == 2 | d_20$V201581 == 3] <- 1

d_20$ses <-( d_20$edu + d_20$inc1 + d_20$homeowner) / 3

final2020 <- d_20 %>%
  dplyr::select(rgc, inc1, female, polint, edu, age, religion, pol_part, pol_partstandard,
                vote, moneyc, ses, blf)

final2020 <- final2020 %>%
  rename(inc = inc1,
         relig = religion,
         campact = pol_part,
         campactstandard = pol_partstandard,
         turnout = vote,
         donate = moneyc)

final2020$year <- rep("2020", 726)

#combine all observational data to make final dataset----
final_observational_data <- rbind(final1984, final1988, final1993, final1996, final2004, final2008,
                                  final2012, final2016, final2020)

#black candidate years (Jackson/Obama) are 1, all others without black candidates are 0
final_observational_data <- final_observational_data %>%
  group_by(year) %>% #do this analysis for each year, so want to group by year
  mutate(JO = case_when( 
    year == "1984" ~ "1", 
    year == "1988" ~ "1",
    year == "1992" ~ "0",
    year == "1996" ~ "0",
    year == "2004" ~ "0",
    year == "2008" ~ "1",
    year == "2012" ~ "1",
    year == "2016" ~ "0",
    year == "2020" ~ "1"
  ))









